package com.ethink.platform.controller.order;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.web.BaseController;
import com.ethink.framework.common.web.context.SPRequestContextHolder;
import com.ethink.framework.common.web.response.CommonResponse;
import com.ethink.mdm.api.AccountFacade;
import com.ethink.mdm.api.LegalCurrencyExchangeRateFacade;
import com.ethink.mdm.api.component.MdmComponent;
import com.ethink.mdm.api.enums.LegalCurrencyEnum;
import com.ethink.mdm.api.info.CompanyAndBankInfo;
import com.ethink.mdm.api.request.LegalCurrencyExchangeRateListByPubDatesRequest;
import com.ethink.mdm.api.response.LegalCurrencyExchangeRateListByPubDatesResponse;
import com.ethink.order.api.TocSaleImportLogFacade;
import com.ethink.order.api.model.TocSaleImportLogModel;
import com.ethink.order.api.request.SaleOrderUpdateStatusRequest;
import com.ethink.order.api.request.TocSaleImportLogAddRequest;
import com.ethink.order.api.request.TocSaleImportLogQueryRequest;
import com.ethink.order.api.response.TocSaleImportLogQueryDetailResponse;
import com.ethink.platform.controller.order.info.TocSaleOrderImportInfo;
import com.ethink.platform.manager.SaleOrderManager;
import com.ethink.platform.manager.TocSaleOrderManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author default
 * @version 1.0.0
 * @ClassName TocSaleOrderController.java
 * @Description
 * @createTime 2022年11月22日 17:23:00
 */

@Tag(name = "TocSaleOrderController", description = " Toc销售单批量 Controller")
@RestController
@Slf4j
@RequestMapping("platform-server/v1/order-service")
public class TocSaleOrderController extends BaseController {

    @Autowired
    private TocSaleImportLogFacade tocSaleImportLogFacade;

    @Autowired
    private TocSaleOrderManager tocSaleOrderManager;

    @Autowired
    private LegalCurrencyExchangeRateFacade legalCurrencyExchangeRateFacade;

    @Autowired
    private AccountFacade accountFacade;

    @Operation(summary = "导入")
    @PostMapping("/tocSale/import")
    public CommonResponse<List<TocSaleOrderImportInfo>> importTocSaleOrder(@RequestParam("file") MultipartFile file) {
        return succeedWith(tocSaleOrderManager.importTocSaleOrder(file));
    }

    @Operation(summary = "新增")
    @PostMapping("/tocSale/add")
    public CommonResponse addTocSaleOrder(@Validated @RequestBody TocSaleImportLogAddRequest request) {
        request.setCreateUserName(SPRequestContextHolder.getCurrentSystemUserRealName());
        request.setCreateUserCode(SPRequestContextHolder.getCurrentSystemUserLoginName());
        LegalCurrencyExchangeRateListByPubDatesRequest currencyRequest = new LegalCurrencyExchangeRateListByPubDatesRequest();
        List<Date> orderTime = CollUtil.getFieldValues(request.getItem(), "orderTime", Date.class);
        orderTime.add(new Date());
        currencyRequest.setPubDates(orderTime);
        currencyRequest.setOrgCurrencyCode(request.getCurrencyCode());
        currencyRequest.setAimCurrencyCode(LegalCurrencyEnum.USD.getCode());
        List<LegalCurrencyExchangeRateListByPubDatesResponse> currencyResponse =
                legalCurrencyExchangeRateFacade.listLegalCurrencyExchangeRateByPubDates(currencyRequest);
        Map<String, BigDecimal> rateMap = new HashMap<>();
        for (LegalCurrencyExchangeRateListByPubDatesResponse response : currencyResponse) {
            rateMap.put(DateUtil.date(response.getPubDate()).toString("yyyy-MM-dd"), response.getRate());
        }
        request.setRateMap(rateMap);
        if (StrUtil.isNotBlank(request.getAccount())) {
            CompanyAndBankInfo companyAndBankInfo = accountFacade.queryCompanyByAccount(request.getAccount());
            request.setBank(companyAndBankInfo.getBank());
            request.setSettleCompanyCode(companyAndBankInfo.getSettleCompanyCode());
            request.setSettleCompanyName(companyAndBankInfo.getSettleCompanyName());
        }
        tocSaleImportLogFacade.addTocSaleOrder(request);
        return succeed();
    }


    @Operation(summary = "分页查询")
    @GetMapping("/tocSale/pageQuery")
    public CommonResponse<List<TocSaleImportLogModel>> pageQueryTocSaleOrder(TocSaleImportLogQueryRequest request) {
        Page<TocSaleImportLogModel> page = tocSaleImportLogFacade.pageQuery(request);
        return succeedWithPage(page);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/tocSale/delete/{id}")
    public CommonResponse deleteTocSaleImport(@PathVariable Long id) {
        tocSaleImportLogFacade.delete(id);
        return succeed();
    }

    @Operation(summary = "详情")
    @GetMapping("/tocSale/detail/{id}")
    public CommonResponse<TocSaleImportLogQueryDetailResponse> queryDetailTocSaleOrder(@PathVariable("id") Long id) {
        TocSaleImportLogQueryDetailResponse response = tocSaleImportLogFacade.queryDetail(id);
        return succeedWith(response);
    }


    @Operation(summary = "修改订单状态")
    @PutMapping("/tocSale/updateStatus")
    public CommonResponse updateTocSaleOrderStatus(@RequestBody @Validated SaleOrderUpdateStatusRequest request) {
        tocSaleImportLogFacade.updateStatus(request);
        return succeed();
    }


}
